package com.sxds.wn.service.biz;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sxds.wn.bo.HotelTypeBo;
import com.sxds.wn.bo.HotelTypeDetailBo;
import com.sxds.wn.dao.biz.CodeDao;
import com.sxds.wn.exception.DataAccessException;
import com.sxds.wn.exception.ParameterException;
import com.sxds.wn.model.CheckedTreeNode;
import com.sxds.wn.model.JsonPage;
import com.sxds.wn.model.Page;
import com.sxds.wn.model.TreeNode;
import com.sxds.wn.service.BaseService;
import com.sxds.wn.service.IService;

/**
 * Description: <p>系统编码服务接口</p>
 * Content Desc:<p>提供系统编码表的查询、修改、删除、修改等功能<p>
 * Copy Right of Personal Tangtao 2007-2020
 * @author Tangtao Email:<a href="mailto:tangtao7481@yahoo.com.cn">tangtao7481@yahoo.com.cn</a>
 * @version 1.0 Create Date:@2009-5-21
 */
public class SystemCodeService extends BaseService implements IService {

	private CodeDao systemCodeDao;
	
	/**
	 * 默认构造方法
	 */
	public SystemCodeService() {
		
	}

	/* (non-Javadoc)
	 * @see com.andy.service.BaseService#checkPermission()
	 */
	@Override
	protected boolean checkPermission() throws SecurityException {
		return false;
	}

	/**
	 * 获取编码目录
	 * @throws DataAccessException 数据库访问异常
	 */
	public void getCodeDir() throws DataAccessException {
		//取出一级编码目录
		List codeDirs = this.systemCodeDao.getHotelType(null);
		if( codeDirs!=null && codeDirs.size()!=0 ){
			int size = codeDirs.size();
			List treeNodes = new ArrayList(size);
			for( int i=0;i<size;i++ ){
				HotelTypeBo hotelTypeBo = (HotelTypeBo)codeDirs.get(i);
				TreeNode treeNode = this.convertToTreeNode(hotelTypeBo, false);
				//取二级资源目录
				treeNodes.add(treeNode);
			}
			//把结果写入的返回对象中
			this.response.setOk(true);
			this.response.getPageAttributes().put("codeDir", treeNodes);
		}
	}
	
	/**
	 * 获取Grid面板数据列表
	 * @throws DataAccessException
	 */
	public void getCodeGrid() throws DataAccessException{
		String parentCode = request.getHttpRequest().getParameter("parentCodeID");
		String codeID = request.getHttpRequest().getParameter("codeID");
		String name = request.getHttpRequest().getParameter("name");
		Map params = new HashMap();
		params.put("parentCode", parentCode);
		params.put("codeID", codeID);
		params.put("name", name);
		String start = request.getHttpRequest().getParameter("start");
		String limit = request.getHttpRequest().getParameter("limit");
		Page page = new Page();
		page.setEnablePage(true);
		page.setPageSize(Integer.parseInt(limit));
		page.setPageNumber(Integer.parseInt(start)/page.getPageSize()+1);
		this.systemCodeDao.loadPage(page,"queryHotelTypeDetailCount","queryHotelTypeDetail", params);
		JsonPage jsonPage = new JsonPage();
		jsonPage.setTotalCount(page.getTotalObjects());
		jsonPage.setList( page.getResults() );
		this.response.setOk(true);
		this.response.getPageAttributes().put("jsonPage",jsonPage);
	}
	
	/**
	 * 
	 * @throws DataAccessException
	 */
	public void addCode() throws DataAccessException{
		HotelTypeDetailBo bo = (HotelTypeDetailBo)parseRequest(HotelTypeDetailBo.class.getName());
		this.systemCodeDao.insertHotelTypeDetail(bo);
		this.response.setOk(true);
		this.response.setResponseMessage("保存增加属性内容成功！");
	}
	
	/**
	 * 
	 * @throws DataAccessException
	 */
	public void updateCode() throws DataAccessException{
		HotelTypeDetailBo bo = (HotelTypeDetailBo)parseRequest(HotelTypeDetailBo.class.getName());
		HotelTypeDetailBo persistBo = this.systemCodeDao.getHotelTypeDetail(bo);
		if( persistBo==null ){
			this.response.setOk(false);
			this.response.setResponseMessage("保存修改属性详细内容失败，可能改记录已被删除！");
		}else{
			persistBo.setFdzlflDm(bo.getFdzlflDm());
			persistBo.setFdzlDm(bo.getFdzlDm());
			persistBo.setFdsxzlDm(bo.getFdsxzlDm());
			persistBo.setSxDown(bo.getSxDown());
			persistBo.setSxUp(bo.getSxUp());
			persistBo.setXs(bo.getXs());
			persistBo.setBz(bo.getBz());
			persistBo.setSxMc(bo.getSxMc());
			this.systemCodeDao.updateHotelTypeDetail(persistBo);
			this.response.setOk(true);
			this.response.setResponseMessage("保存修改属性详细内容成功！");
		}
		throw new DataAccessException("测试异常信息！");
	}
	/**
	 * 把SystemCode对象转换TreeNode对象
	 * @param umResource UmResource
	 * @return java.util.List<TreeNode>
	 */
	private TreeNode convertToTreeNode(HotelTypeBo hotelTypeBo,boolean checked){
		TreeNode treeNode = (checked ? new CheckedTreeNode():new TreeNode());
		treeNode.setId(hotelTypeBo.getFdzlDm());
		treeNode.setText(hotelTypeBo.getMc());
		return treeNode;
	}

	/* (non-Javadoc)
	 * @see com.andy.service.BaseService#validateParameters()
	 */
	@Override
	protected boolean validateParameters() throws ParameterException {
		return false;
	}

	/**
	 * @param systemCodeDao the systemCodeDao to set
	 */
	public void setSystemCodeDao(CodeDao systemCodeDao) {
		this.systemCodeDao = systemCodeDao;
	}
	
}
